using System;
using System.Runtime.Intrinsics;
using CUE4Parse.UE4.VirtualFileSystem;
using static System.Runtime.Intrinsics.X86.Aes;
using static System.Runtime.Intrinsics.Vector128;

namespace CUE4Parse.GameTypes.MJS.Encryption.Aes;

/// <summary>
/// Reversed by Spiritovod
/// </summary>
public static class MonsterJamShowdownAes
{
    // 0x38DD69D33C5C027A9C37F8A3AE04B7D0DBA9F51494E60E5B598CDD5ECCBD5CE2
    private static readonly Vector128<byte>[] RoundKeys =
    [
        Create(0xCF, 0x84, 0x41, 0x0F, 0xAD, 0x92, 0x48, 0x04, 0xB3, 0x6C, 0xEA, 0x11, 0x31, 0x80, 0x1F, 0x97),
        Create(0x04, 0xCA, 0xE1, 0x5C, 0x6F, 0x2F, 0x81, 0x95, 0x54, 0x98, 0xD3, 0x71, 0x1A, 0x31, 0x7F, 0xEC),
        Create(0xA2, 0xDA, 0x2C, 0xD0, 0xD6, 0xB7, 0x51, 0x46, 0xDA, 0x1E, 0xC8, 0x5B, 0x6C, 0x98, 0x21, 0xC8),
        Create(0xE5, 0xE0, 0x0B, 0x02, 0x6B, 0xE5, 0x60, 0xC9, 0x3B, 0xB7, 0x52, 0xE4, 0x4E, 0xA9, 0xAC, 0x9D),
        Create(0xA0, 0xDF, 0xA8, 0xE2, 0x74, 0x6D, 0x7D, 0x96, 0x0C, 0xA9, 0x99, 0x1D, 0xB6, 0x86, 0xE9, 0x93),
        Create(0x4B, 0x3E, 0x1B, 0xF2, 0x8E, 0x05, 0x6B, 0xCB, 0x50, 0x52, 0x32, 0x2D, 0x75, 0x1E, 0xFE, 0x79),
        Create(0xEE, 0xB8, 0x47, 0x8D, 0xD4, 0xB2, 0xD5, 0x74, 0x78, 0xC4, 0xE4, 0x8B, 0xBA, 0x2F, 0x70, 0x8E),
        Create(0x44, 0xDB, 0x07, 0x25, 0xC5, 0x3B, 0x70, 0x39, 0xDE, 0x57, 0x59, 0xE6, 0x25, 0x4C, 0xCC, 0x54),
        Create(0xE1, 0x79, 0x97, 0x37, 0x3A, 0x0A, 0x92, 0xF9, 0xAC, 0x76, 0x31, 0xFF, 0xC2, 0xEB, 0x94, 0x05),
        Create(0x70, 0x89, 0xDD, 0x55, 0x81, 0xE0, 0x77, 0x1C, 0x1B, 0x6C, 0x29, 0xDF, 0xFB, 0x1B, 0x95, 0xB2),
        Create(0x88, 0xE7, 0x95, 0xE3, 0xDB, 0x73, 0x05, 0xCE, 0x96, 0x7C, 0xA3, 0x06, 0x6E, 0x9D, 0xA5, 0xFA),
        Create(0x2D, 0xC9, 0x78, 0xFF, 0xF1, 0x69, 0xAA, 0x49, 0x9A, 0x8C, 0x5E, 0xC3, 0xE0, 0x77, 0xBC, 0x6D),
        Create(0x79, 0x7F, 0x6E, 0x35, 0x53, 0x94, 0x90, 0x2D, 0x4D, 0x0F, 0xA6, 0xC8, 0xF8, 0xE1, 0x06, 0xFC),
        Create(0x27, 0x07, 0xC8, 0xB7, 0xDC, 0xA0, 0xD2, 0xB6, 0x6B, 0xE5, 0xF4, 0x8A, 0x7A, 0xFB, 0xE2, 0xAE),
        Create(0xDB, 0xA9, 0xF5, 0x14, 0x94, 0xE6, 0x0E, 0x5B, 0x59, 0x8C, 0xDD, 0x5E, 0xCC, 0xBD, 0x5C, 0xE2),
    ];

    private static void DecryptWithRoundKeys(byte[] input, int index, Vector128<byte>[] roundkeys)
    {
        var state = Create(input, index);
        var rounds = roundkeys.Length - 1;
        state = Xor(state, roundkeys[0]);
        for (var i = 1; i < rounds; i++)
        {
            state = Decrypt(state, roundkeys[i]);
        }

        state = DecryptLast(state, roundkeys[rounds]);
        state.CopyTo(input, index);
    }

    public static byte[] MonsterJamShowdownDecrypt(byte[] bytes, int beginOffset, int count, bool isIndex, IAesVfsReader reader)
    {
        if (bytes.Length < beginOffset + count)
            throw new IndexOutOfRangeException("beginOffset + count is larger than the length of bytes");
        if (count % 16 != 0)
            throw new ArgumentException("count must be a multiple of 16");
        if (reader.AesKey == null)
            throw new NullReferenceException("reader.AesKey");

        var output = new byte[count];
        Array.Copy(bytes, beginOffset, output, 0, count);

        for (var i = 0; i < count / 16; i++)
        {
            DecryptWithRoundKeys(output, i * 16, RoundKeys);
        }

        return output;
    }
}
